package com.zmn.plat.admin.controller.online.service.group;

import com.alibaba.fastjson.JSONObject;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.model.entity.online.service.group.OnlineServiceGroup;
import com.zmn.plat.model.entity.online.service.group.OnlineServiceGroupQuery;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 在线客服分组基本信息表
 *
 * @author linfeng
 *
 * @since 2021-03-26 17:21:53
 */
@RestController
@RequestMapping("/online/service/group/")
public class OnlineServiceGroupModifyController extends OnlineServiceGroupBaseController {

    /**
     * 新增修改
     * 
     * @author linfeng
     * @param onlineServiceGroup
     * @since 2021-03-26 17:21:53
     */
    @RequestMapping(value = "addEdit")
    @RequiresPermissions("/online/service/group/detail.action")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody OnlineServiceGroup onlineServiceGroup) {
        Date now = DateUtil.getNow();
        String operator = getMcStaffRealName();
        Integer groupId = onlineServiceGroup.getGroupId();

        try {
            OnlineServiceGroupQuery query = new OnlineServiceGroupQuery();
            query.setSysId(onlineServiceGroup.getSysId());
            query.setGroupId(groupId);
            query.setSubCompanyIdList(StringUtil.convertStrsToList(onlineServiceGroup.getSubCompanyId()));
            if (Objects.equals(GlobalConsts.YES, onlineServiceGroup.getDefaultType())) {
                Boolean flag = onlineServiceGroupBService.judgeHasDefaultServiceGroup(query);
                if (flag) {
                    return ResponseDTO.fail("一个系统+一个子公司最多可设置1个默认客服组，请调整后再试！");
                }
            }

            if (NumberUtil.isNullOrZero(groupId)) {
                onlineServiceGroup.setCreater(operator);
                onlineServiceGroup.setCreateTime(now);
                onlineServiceGroup.setUpdater(operator);
                onlineServiceGroup.setUpdateTime(now);
                onlineServiceGroupService.insert(onlineServiceGroup);
            } else {
                onlineServiceGroup.setUpdater(operator);
                onlineServiceGroup.setUpdateTime(now);
                onlineServiceGroupService.updateByKey(onlineServiceGroup);
            }

            return ResponseDTO.success(true,"操作成功！");
        } catch (Exception ex) {
            logger.error("[zmn]新增/修改在线客服分组基本信息表失败,onlineServiceGroup:{}", JSONObject.toJSONString(onlineServiceGroup));
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 修改状态
     * 
     * @author linfeng
     * @param ids
     * @param status
     * @since 2021-03-26 17:21:53
     */
    @RequestMapping(value = "updateStatus")
    @RequiresPermissions("/online/service/group/updateStatus.action")
    public ResponseDTO updateStatus(HttpServletRequest request, @RequestParam(value = "ids", defaultValue = "") String ids, @RequestParam(value = "status", defaultValue = "0") Integer status) {
        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }

        if (NumberUtil.isNullOrZero(status)) {
            return ResponseDTO.fail("请设置状态！");
        }

        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);

        onlineServiceGroupService.updateStatusBatchByKey(arrayIds, status, getMcStaffRealName());

        return ResponseDTO.success(true, "操作成功！");
    }

}
